//需要校验 手机号格式、密码格式、勾选协议
let flag1 = flag2 = flag3 = false;
let mobile = document.querySelector('.mobile');
let password = document.querySelector('.password');
let checkbox = document.querySelector('#checkbox');
let regBtn = document.querySelector('#reg-button');

mobile.onblur = ()=>{
    let regular =  /^1[3|4|5|7|8][0-9]{9}$/;
    (regular.test(mobile.value) == true) ? (flag1 = true) : (alert('输入的手机号格式不对'));
    isOk();
}

password.onblur = ()=>{
    let regular = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
    (regular.test(password.value) == true) ? (flag2 = true) : (alert('输入的密码格式不对'));
    isOk();
}

checkbox.onclick = ()=>{
    (checkbox.checked == true) ? (flag3 = true) : (alert('请阅读协议并勾选'));
    isOk();
}

function isOk(){
    (flag1 && flag2 && flag3) ? (regBtn.disabled = false) : (regBtn.disabled = true);
}

regBtn.onclick = ()=>{
    console.log('ok');
    ajax({
        url:'http://phpclub.org.cn:8080/api/user/login',
        type:'POST',
        data:{
            mobile:mobile.value,
            password:password.value
        },
        contentType:'application/json',
        success:(response)=>{
            console.log(response);
            if(response.code == 0){
                console.log(response,'ok');
                let time = 7 * 24 * 3600;
                setCookie('userInfo',JSON.stringify(response.data),time);
                let callback = getUrlParam('callback') ? decodeURIComponent(getUrlParam('callback')) : 'index.html';
                location.href = callback;
            }else{
                alert(response.msg);
            }
        }
    })
}